package cn.com.czpb.client.module.personal.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.com.czpb.client.R;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.model.usercenter.HistoryBean;
import cn.com.czpb.client.module.news.activity.NewsAlbum_Activity;
import cn.com.czpb.client.module.news.activity.NewsDetailActivity;
import cn.com.czpb.client.module.news.activity.NewsVideoActivity;
import cn.com.czpb.client.module.news.activity.ZhuantiActivityNew;
import cn.com.czpb.client.utils.AAnim;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.StringUtils;
import cn.com.czpb.client.utils.TUtils;


public class HistoryAdapter extends BaseMultiItemQuickAdapter<HistoryBean, BaseViewHolder> {
    private Context context;
    private boolean flag = false;
    private SPUtil spu;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public HistoryAdapter(List<HistoryBean> data, Context context) {
        super(data);
        this.context = context;
        spu = SPUtil.getInstance();
        addItemType(HistoryBean.LEFTPIC, R.layout.news_list_item_layout);//左图
        addItemType(HistoryBean.NOPIC, R.layout.news_list_item_nopic_layout);//无图
        addItemType(HistoryBean.BIGPIC, R.layout.news_list_bigitem_layout);//大图
        addItemType(HistoryBean.THRPIC, R.layout.news_3_item_layout); //3图

    }

    @Override
    protected void convert(final BaseViewHolder helper, final HistoryBean item) {
        helper.setText(R.id.newsitem_title, item.getTitle());//title
        helper.setText(R.id.newsitem_copyfrom, item.getCopyfrom());
        helper.setText(R.id.comment_num_id, item.getComcount() + "跟帖");//评论数量);
        if (!StringUtils.isEmpty(item.getNewsflag())){
            helper.setVisible(R.id.img_newstype_id,true);
            helper.setText(R.id.img_newstype_id, item.getNewsflag());//title
        }else{
            helper.setVisible(R.id.img_newstype_id,false);
        }
        helper.getView(R.id.newsItem_root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TUtils.toast("点击");

            }
        });
        switch (helper.getItemViewType()) {
            case HistoryBean.THRPIC:
                helper.setText(R.id.praise_num_id,item.getPraisecount());
                if (spu.getShowImage().equals("0")) {
                    if (item.getImgs().size() > 3) {
                        Glide.with(mContext).load(item.getImgs().get(0)).into((ImageView) helper.getView(R.id.news_3_item1));

                        Glide.with(mContext).load(item.getImgs().get(1)).into((ImageView) helper.getView(R.id.news_3_item2));
                        Glide.with(mContext).load(item.getImgs().get(2)).into((ImageView) helper.getView(R.id.news_3_item3));
                    }
                }

                helper.getView(R.id.newsItem_root).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OnClick(item);
                    }
                });
                break;
            case HistoryBean.LEFTPIC:
                helper.setText(R.id.praise_num_id,item.getPraisecount());
                if (!item.getRtype().equals("6")) {
                    helper.getView(R.id.img_play_id).setVisibility(View.GONE);
                }
                if (spu.getShowImage().equals("0")) {
                    if(item.getImgs().size()>0) {
                        Glide.with(mContext).load(item.getImgs().get(0)).into((ImageView) helper.getView(R.id.newsitem_img));
                    }

                }
                helper.setText(R.id.newsitem_title, item.getTitle())
                        .addOnClickListener(R.id.newsItem_root);
                helper.getView(R.id.newsItem_root).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OnClick(item);
                    }
                });
                break;

            case HistoryBean.NOPIC:
                helper.setText(R.id.praise_num_id,item.getPraisecount());
                helper.setText(R.id.newsitem_title, item.getTitle())
                        .addOnClickListener(R.id.newsItem_root);
                helper.getView(R.id.newsItem_root).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OnClick(item);
                    }
                });
                break;
            case HistoryBean.BIGPIC:
                helper.setText(R.id.praise_num_id,item.getPraisecount());
                if (!item.getRtype().equals("6")) {
                    helper.getView(R.id.img_play_id).setVisibility(View.GONE);
                }
                if (spu.getShowImage().equals("0")) {
                    Glide.with(mContext).load(item.getImgs().get(0)).into((ImageView) helper.getView(R.id.newsitem_img));

                }
                helper.setText(R.id.newsitem_title, item.getTitle())
                        .addOnClickListener(R.id.newsItem_root);
                helper.getView(R.id.newsItem_root).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OnClick(item);
                    }
                });
                break;


        }
    }

    private void OnClick(HistoryBean item) {
        Intent intent = new Intent();
        NewsBean nb = new NewsBean();
        nb.setNid(item.getNid());
        nb.setImgs(item.getImgs());
        nb.setTid(item.getTid());

        intent.putExtra("newbean", nb);
        intent.putExtra("nid", item.getNid());
        intent.putExtra("from", "history");
        intent.putExtra("rvalue", item.getRvalue());
        if ("1".equals(item.getRtype())) {

            flag = true;
            intent.setClass(mContext, NewsDetailActivity.class);
        } else if ("2".equals(item.getRtype())) {

            flag = true;
            intent.setClass(mContext, NewsAlbum_Activity.class);
        } else if ("4".equals(item.getRtype())) {

            flag = true;
            intent.setClass(mContext, ZhuantiActivityNew.class);
        } else if ("6".equals(item.getRtype())) {

            flag = true;
            intent.setClass(mContext, NewsVideoActivity.class);
        } else {
            flag = true;
            intent.setClass(mContext, NewsDetailActivity.class);
        }
        if (flag) {
            mContext.startActivity(intent);
            AAnim.ActivityStartAnimation((Activity) context);
        }
    }

}
